package com.lgz.dbcourse.service.impl;

import com.lgz.dbcourse.dto.*;
import com.lgz.dbcourse.entity.MovieEntity;
import com.lgz.dbcourse.entity.MovieScheduleEntity;
import com.lgz.dbcourse.entity.StaffEntity;
import com.lgz.dbcourse.mapper.HallMapper;
import com.lgz.dbcourse.mapper.MovieMapper;
import com.lgz.dbcourse.mapper.MovieScheduleMapper;
import com.lgz.dbcourse.mapper.StaffMapper;
import com.lgz.dbcourse.service.StaffService;
import com.lgz.dbcourse.util.CommonUtil;
import com.lgz.dbcourse.util.DbUtil;
import org.apache.ibatis.exceptions.PersistenceException;

/**
 * @Author: LGZ
 * @Date: 2021/4/10 16:20
 * @Description:
 */
public class StaffServiceImpl implements StaffService {

    @Override
    public ResultSet login(StaffInputDTO staffInputDTO) {
        StaffOutputDTO staffOutputDTO = DbUtil.getSqlSession().getMapper(StaffMapper.class).getStaffByPassword(CommonUtil.getMapperFactory().getMapperFacade().map(staffInputDTO, StaffEntity.class));
        if (null != staffOutputDTO) {
            return CommonUtil.resultReturn(staffOutputDTO);
        } else {
            return CommonUtil.resultReturn(null, 400, "用户名与密码不匹配");
        }
    }

    @Override
    public ResultSet addMovie(MovieDTO movieDTO) {
        try {
            DbUtil.getSqlSession(true).getMapper(MovieMapper.class).insertMovie(CommonUtil.getMapperFactory().getMapperFacade().map(movieDTO, MovieEntity.class));
            return CommonUtil.resultReturn(null);
        } catch (PersistenceException e) {
            return CommonUtil.resultReturn(null, 500, "数据错误");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonUtil.resultReturn(null, 500, "内部错误，请联系管理员");
        }
    }

    @Override
    public ResultSet updateMovie(MovieDTO movieDTO) {
        DbUtil.getSqlSession(true).getMapper(MovieMapper.class).updateMovie(CommonUtil.getMapperFactory().getMapperFacade().map(movieDTO, MovieEntity.class));
        return CommonUtil.resultReturn(null);
    }

    @Override
    public ResultSet deleteMovie(MovieDTO movieDTO) {
        DbUtil.getSqlSession(true).getMapper(MovieMapper.class).deleteMovie(CommonUtil.getMapperFactory().getMapperFacade().map(movieDTO, MovieEntity.class));
        return CommonUtil.resultReturn(null);
    }

    @Override
    public ResultSet getAllMovie() {
        return CommonUtil.resultReturn(DbUtil.getSqlSession().getMapper(MovieMapper.class).getAllMovie());
    }

    @Override
    public ResultSet getAllHall() {
        return CommonUtil.resultReturn(DbUtil.getSqlSession().getMapper(HallMapper.class).getAllHall());
    }

    @Override
    public ResultSet addSchedule(MovieScheduleDTO movieScheduleDTO) {
        try {
            DbUtil.getSqlSession(true).getMapper(MovieScheduleMapper.class).addSchedule(CommonUtil.getMapperFactory().getMapperFacade().map(movieScheduleDTO, MovieScheduleEntity.class));
            return CommonUtil.resultReturn(null);
        } catch (PersistenceException e) {
            return CommonUtil.resultReturn(null, 500, "数据错误");
        } catch (Exception e) {
            e.printStackTrace();
            return CommonUtil.resultReturn(null, 500, "内部错误，请联系管理员");
        }
    }

    @Override
    public ResultSet getSchedule() {
        return CommonUtil.resultReturn(DbUtil.getSqlSession().getMapper(MovieScheduleMapper.class).getSchedulePlaying());
    }

    @Override
    public ResultSet getMovie(MovieDTO movieDTO) {
        return CommonUtil.resultReturn(DbUtil.getSqlSession().getMapper(MovieMapper.class).getMovieById(CommonUtil.getMapperFactory().getMapperFacade().map(movieDTO, MovieEntity.class)));
    }
}
